<?php  
   class LoginController extends Controller{
       
	   
	    public function filters(){
		     return array(
				'accessControl',
			 );
		 }
	   
        public function actionIndex(){
			  
			 if(!Yii::app()->user->isGuest)
			  $this->redirect(Yii::app()->user->index);
		    $this->renderPartial('index',array('model'=>new LoginForm));
		   
		}
		/*
		   @Override the method from CController to avoid request loop
		*/
		public function beforeAction($action){
			Yii::app()->params['static'] = Yii::app()->request->baseUrl.'/';
		    return true;
		}
		
		public function  actionAuth(){
		
		     if(!Yii::app()->user->isGuest)
			  $this->redirect(Yii::app()->user->index);
			$loginModel = new LoginForm();
			$loginModel->attributes = $_POST;
			$loginModel->validate();
			if($loginModel->login()){
			   $this->redirect(Yii::app()->user->index);
			   
			}else{
			   $this->renderPartial('index',array('model'=>$loginModel));
			}
			
		}
		public function actionLogout(){
		    Yii::app()->user->logout();
			$this->redirect(Yii::app()->createUrl('login/index'));
		}
		public function actionResetpassword(){
			if(Yii::app()->request->requestType == 'POST'){
				
				$user = User::model()->findByPk(Yii::app()->user->getId());
				if($user){
					$user->password = $_POST['newPassword'];
					$user->isNew = 0;
					if($user->save()){
						Yii::app()->user->setFlash('update','密码更新成功');
						Yii::app()->user->setState('isNew',$user->isNew);
						$this->redirect(Yii::app()->createUrl('stubasicinfo/index'));
						
					}else{
						Yii::app()->user->setFlash('update','密码更新失败');
						
					}
				}else{
					 Yii::app()->user->setFlash('update','用户不存在');
					 $this->renderPartial('reset');
					
				}
			}else{
				
		        $this->renderPartial('reset');
			}
		}
		public function actionTest(){
		   var_dump(Yii::app()->user->getName());
		   if(Yii::app()->user->rights==1)
		   var_dump(Yii::app()->user->project);
		}
							
}
?>